<?php
class AnnouncementsController extends AppController {

	var $name = 'Announcements';

	function index() {
		$this->Announcement->recursive = 0;
		$this->set('announcements', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid announcement', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('announcement', $this->Announcement->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Announcement->create();
			if ($this->Announcement->save($this->data)) {
				$this->Session->setFlash(__('The announcement has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The announcement could not be saved. Please, try again.', true));
			}
		}
		$schools = $this->Announcement->School->find('list');
		$this->set(compact('schools'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid announcement', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Announcement->save($this->data)) {
				$this->Session->setFlash(__('The announcement has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The announcement could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Announcement->read(null, $id);
		}
		$schools = $this->Announcement->School->find('list');
		$this->set(compact('schools'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for announcement', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Announcement->delete($id)) {
			$this->Session->setFlash(__('Announcement deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Announcement was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function admin_index() {
		$this->Announcement->recursive = 0;
		$this->set('announcements', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid announcement', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('announcement', $this->Announcement->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Announcement->create();
			if ($this->Announcement->save($this->data)) {
				$this->Session->setFlash(__('The announcement has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The announcement could not be saved. Please, try again.', true));
			}
		}
		$schools = $this->Announcement->School->find('list');
		$this->set(compact('schools'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid announcement', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Announcement->save($this->data)) {
				$this->Session->setFlash(__('The announcement has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The announcement could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Announcement->read(null, $id);
		}
		$schools = $this->Announcement->School->find('list');
		$this->set(compact('schools'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for announcement', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Announcement->delete($id)) {
			$this->Session->setFlash(__('Announcement deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Announcement was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	/**
	* ra = requestAction: for call from view directly
	*/
	/**
	*	return all records of Announcement table
	*/
	function ra_all_announcements(){
		return $this->Announcement->find('all', array('recursive' => 0));
	}
	/**
	*	check the loggin is represent of school
	*/
	function ra_check_represent_school($user_id){
		$this->loadModel('User');
		$user = $this->User->find('first', array(
			'conditions' => array('User.id' => $user_id),
			'recursive' => 1
		));
		if($user['Group']['role'] == 'represents') 
			return true;
		return false;
	}
	/**
	*	return the 5 latest announcements 
	*/
	function ra_latest_announcements(){
		return $this->Announcement->find('all', array(
			'condition' => array('Announcement.published' => 'YES'),
			'recursive' => 0, 
			'order' => array('Announcement.modified' => 'DESC'), 
			'limit' => 5
		));
	}
	
}
?>